<template>
  <div class="app-container calendar-list-container">
    <div v-show="!opShow && !techShow">
      <buttons :showQuery="true" :showBack="true" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="global.modelName" :permissions="permissions" @add="doAdd">
      </buttons>
      <grid :columns="columns" :multi="false" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="75" @change="rowCilck" :queryEntity="{materialCode:materialCode}"
        :pagination="true" @edit="doEdit" @view="doView" @delete="doDelete" @open="doOpen" @close="doClose" @tech="doTech" :permissions="permissions"></grid>
    </div>
    <op ref="op" :visible="opShow" @cancel="cancel"></op>
    <tech ref="tech" :visible="techShow" @cancel="cancel"></tech>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import op from './op'
import tech from './tech'
import { Message, MessageBox } from 'element-ui'
import { initMenu } from "@/util/util";
import { resolveUrlPath } from "@/util/util";
import { mapGetters } from 'vuex'
import { YesOrNoFormatter } from '@/const/dict'
import { bgColor } from '@/const/bgColor'

export default {
  components: { grid, buttons, op, tech },
  data () {
    return {
      queryUrl: url.MODEL_LIST_URL,
      disabled: true,
      opShow: false,
      techShow: false,
      materialCode: '',
      materialName: '',
      columns: [
        {
          label: '操作', width: 230, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '编辑', clickName: 'edit', auth: 'model_edit' },
            { name: '查看', clickName: 'view', auth: 'model_view' },
            { name: '工艺', clickName: 'tech', auth: 'model_open' },
            { name: '开启', clickName: 'open', auth: 'model_open', confirm: true },
            { name: '关闭', clickName: 'close', auth: 'model_close', confirm: true }
          ]
        },
        { label: this.global.modelName, width: 100, prop: 'modelName', align: 'left' },
        { label: this.global.modelProp, width: 100, prop: 'modelProp', align: 'left' },
        { label: '名称', width: 380, prop: 'name', align: 'left' },
        { label: '条形码', width: 180, prop: 'barCode', align: 'center' },
        { label: '是否启用', width: 80, prop: 'status', align: 'center', formatter: YesOrNoFormatter, template: 'bgColor', bgColor: bgColor().YesOrNo },
        { label: '创建人', width: 120, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'model_add', ctr: false },
        // { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'model_edit', ctr: true },
        // { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'model_view', ctr: true },
        // { name: '工艺', type: 'primary', icon: 'edit', click: 'tech', auth: 'model_open', ctr: true },
        // { name: '开启', type: 'primary', icon: 'edit', click: 'open', auth: 'model_open', ctr: true, confirm: true },
        // { name: '关闭', type: 'primary', icon: 'edit', click: 'close', auth: 'model_close', ctr: true, confirm: true }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'permissions'
    ])
  },
  created () {
    this.materialCode = this.$route.query.materialCode
    this.materialName = this.$route.query.materialName
  },
  methods: {
    doAdd () {
      this.templateOp().then((data) => {
        this.opShow = true
        this.$refs['op'].init('新增', { materialCode: this.materialCode, name: this.materialName }, false, url.MODEL_ADD_URL)
      })
    },
    doEdit (row) {
      //  const row = this.$refs.mainGrid.getSelectOne()
      // if (row) {
      this.viewOp(row.modelCode).then((data) => {
        this.opShow = true
        this.$refs['op'].init('编辑', data, false, url.MODEL_EDIT_URL)
      })
      //  }
    },
    doView (row) {
      //  const row = this.$refs.mainGrid.getSelectOne()
      // if (row) {
      this.viewOp(row.modelCode).then((data) => {
        this.opShow = true
        this.$refs['op'].init('查看', data, true)
      })
      // }
    },
    doDelete (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      // if (row) {
      // 删除数据
      this.deleteOp(row.modelCode).then((data) => {
        if (data) {
          this.$refs['mainGrid'].reload()
        }
      })
      // }
    },
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
      this.techShow = false
    },
    doTech (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.techShow = true
        this.$refs['tech'].init('编辑工艺', row.modelCode)
      }
    },
    deleteOp (code) {
      return request({
        url: url.MODEL_DELETE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'modelCode': code }
      })
    },
    templateOp () {
      return request({
        url: url.TEMPLATE_USABLE_URL,
        method: 'post',
        data: { 'key': 'key' },
      })
    },
    viewOp (modelCode) {
      return request({
        url: url.MODEL_VIEW_URL,
        method: 'post',
        params: { modelCode },
        data: { modelCode }
      })
    },
    openOp (code) {
      return request({
        url: url.MODEL_OPEN_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'modelCode': code }
      })
    },
    closeOp (code) {
      return request({
        url: url.MODEL_CLOSE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'modelCode': code }
      })
    },
    doOpen (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.openOp(row.modelCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doClose (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.closeOp(row.modelCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
